// 方式 1
var rotate = function (nums, k) {
  nums.splice(0, 0, ...nums.splice(nums.length - k));
}

// 使用反转
var rotate = function (nums, k) {
  k = k % nums.length;
  nums = reverse(nums, 0, nums.length - 1);
  nums = reverse(nums, 0, k - 1);
  nums = reverse(nums, k, nums.length - 1);
}

function reverse (nums, start, end) {
  while (start < end) {
    let temp = nums[start];
    nums[start] = nums[end];
    nums[end] = temp;
    start++;
    end--;
  }
  return nums;
}